/* 
 * Copyright (c) Sergey B. Berezin (s_berezin@cs.msu.su), 2002, 2003.
 *
 * You may freely distribute or modify this source code retaining the 
 * above copyright notice.
 *
 * This software is provided "as-is" and without guarantee or warrantee 
 * of any kind, expressed or implied :)
 */

using System;
using System.Collections;

namespace UVS.Base {

	/// <summary>
	/// Compares objects that implements IComparable
	/// </summary>
	public class DefaultComparer : IComparer {
	
		public int Compare(object a,object b) {
			IComparable ic = a as IComparable;
			if(ic == null)
				throw new ArgumentException("First object does not implement IComparable");
			return ic.CompareTo(b);
		}
	}

	/// <summary>
	/// Base for sorted collection. All members are protected so public data
	/// access should be added in derived classes.
	/// </summary>
	public abstract class ReadOnlySortedCollectionBase : ReadOnlyCollectionBase {

		protected IComparer comparer = null;

		protected ReadOnlySortedCollectionBase() {
			comparer = new DefaultComparer();
		}

		protected ReadOnlySortedCollectionBase(IComparer c) {
			comparer = c;
		}

		protected void Add(Object obj) {
			int idx = 0;
			while(idx < Count && comparer.Compare(InnerList[idx],obj) < 0)
				idx++;
			InnerList.Insert(idx,obj);
		}

		protected void Remove(Object obj) {
			InnerList.Remove(obj);
		}

		public void Sort() {
			// TODO: Add code for sorting collection here
		}
	}
}